#include "fxcc/imgui/Editor.h"
#include "fxcc/core/Dir.h"
#include "fxcc/core/graph/common/Maps.h"
#include "fxcc/core/graph/common/TextureImporter.h"
#include "fxcc/core/graph/common/ModelImporter.h"

using namespace fxcc::graph::common;
using namespace fxcc::imgui;


template<>
bool Editor::Edit<fxcc::graph::common::MeshMap>(const std::string& l, fxcc::graph::common::MeshMap& e)
{
	bool flag{ 0 };
	bool combo = ImGui::BeginCombo(l.c_str(), e.m_Index.m_Name.c_str());

	if (combo)
	{

		for (const auto& [key, value] : ModelImporter::m_ModelMetaData.m_ModelMetaCache)
		{
			ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 1.0f, 0.5f, 1.0f));
			ImGui::Text(key.c_str());
			ImGui::PopStyleColor();

			for (const auto& value2 : value.m_MeshMetaIndexes)
			{
				ImGui::PushID(&value2);
				const std::string& itemLabel = value2.m_Name;
				bool selectable = ImGui::Selectable(itemLabel.c_str(), e.m_Index.m_FileId == value2.m_FileId);
				if (selectable && e.m_Index != value2)
				{
					e.m_Index = value2;
					flag = true;
				}
				ImGui::PopID();
			}

		}

		ImGui::EndCombo();
	}

	if (flag)
	{
		e.OnLoad();
	}
	return flag;

}


template<>
bool Editor::Edit<fxcc::graph::common::SkinnedMeshMap>(const std::string& l, fxcc::graph::common::SkinnedMeshMap& e)
{
	bool flag{ 0 };

	bool combo = ImGui::BeginCombo(l.c_str(), e.m_Index.m_Name.c_str());

	if (combo)
	{

		for (const auto& [key, value] : ModelImporter::m_ModelMetaData.m_ModelMetaCache)
		{
			ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 1.0f, 0.5f, 1.0f));
			ImGui::Text(key.c_str());
			ImGui::PopStyleColor();

			for (const auto& value2 : value.m_SkinnedMetaIndexes)
			{
				ImGui::PushID(&value2);
				const std::string& itemLabel = value2.m_Name;
				bool selectable = ImGui::Selectable(itemLabel.c_str(), e.m_Index.m_FileId == value2.m_FileId);
				if (selectable && e.m_Index != value2)
				{
					e.m_Index = value2;
					flag = true;
				}
				ImGui::PopID();
			}

		}

		ImGui::EndCombo();
	}
	if (flag)
	{
		e.OnLoad();
	}
	return flag;
}

template<>
bool Editor::Edit<fxcc::graph::common::BoneFileMap>(const std::string& l, fxcc::graph::common::BoneFileMap& e)
{
	bool flag{ 0 };

	bool combo = ImGui::BeginCombo(l.c_str(), e.m_Index.m_Name.c_str());

	if (combo)
	{

		for (const auto& [key, value] : ModelImporter::m_ModelMetaData.m_ModelMetaCache)
		{
			ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 1.0f, 0.5f, 1.0f));
			ImGui::Text(key.c_str());
			ImGui::PopStyleColor();

			for (const auto& value2 : value.m_BoneMetaIndexes)
			{
				ImGui::PushID(&value2);
				const std::string& itemLabel = value2.m_Name;
				bool selectable = ImGui::Selectable(itemLabel.c_str(), e.m_Index.m_FileId == value2.m_FileId);
				if (selectable)
				{
					e.m_Index = value2;
					flag = true;

				}
				ImGui::PopID();
			}
		}

		ImGui::EndCombo();
	}
	if (flag)
	{
		e.OnLoad();
	}
	return flag;

}

template<>
bool Editor::Edit<fxcc::graph::common::MaterialViewMap>(const std::string& l, fxcc::graph::common::MaterialViewMap& e)
{
	bool flag{ 0 };

	bool combo = ImGui::BeginCombo(l.c_str(), e.m_Index.m_Name.c_str());

	auto& index = e.m_Index;
	if (combo)
	{

		for (const auto& [key, value] : ModelImporter::m_ModelMetaData.m_ModelMetaCache)
		{
			ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 1.0f, 0.5f, 1.0f));
			ImGui::Text(key.c_str());
			ImGui::PopStyleColor();

			for (const auto& value2 : value.m_RealMaterialMetaIndexes)
			{
				ImGui::PushID(&value2);
				const std::string& itemLabel = value2.m_Name;
				bool selectable = ImGui::Selectable(itemLabel.c_str(), e.m_Index.m_FileId == value2.m_FileId);
				if (selectable && index != value2)
				{
					index = value2;
					flag = true;
				}
				ImGui::PopID();
			}

		}
		ImGui::EndCombo();
	}

	if (ImGui::BeginDragDropTarget()) {
		if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("File")) {
			using T = fxcc::File;
			const T file = *(const T*)payload->Data;

			switch (file.m_FileType)
			{
			case fxcc::FileType::_RealMat_:
				if (index.m_TargetFilePath != file.m_RelativePath)
				{
					index.m_Name = file.m_FileName;
					index.m_TargetFilePath = file.m_RelativePath;
					flag = true;
				}
				break;
			default:
				break;
			}
		}
		ImGui::EndDragDropTarget();
	}
	if (flag)
	{
		ztclog::info("real material on load");
		e.OnLoad();
	}
	return flag;

}